package com.zhiyou.wisdom.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.wisdom.pojo.Schema1;
import com.zhiyou.wisdom.service.SchemaService;

@RestController
@RequestMapping("/schema")
public class SchemaController {
	@Autowired
	private SchemaService schemaService;
	
	/**
	 * 查看当前用户的待办事项
	 * @param uid
	 * @return
	 */
	@GetMapping(value="/getAll/{uid}")
	public List<Schema1> getAllSchema(@PathVariable Integer uid){
		return schemaService.selectByExample(uid);
	}
	
	/**
	 * 返回state:1新增成功    state:0 新增失败
	 * @param record
	 * @param map
	 * @return
	 */
	@GetMapping(value="/addSchema")
	public Map<String, Object> insertSchema(Schema1 record){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("state", schemaService.insert(record));
		map.put("time",record.getsTime());
		return map;
	}
	
	@GetMapping(value="/update/schema/{uid}")
	public Map<String, Integer> updateSchema(@PathVariable Integer uid){
		Map<String, Integer> map=new HashMap<>();
		Schema1 schema1=new Schema1();
		schema1.setsUserId(uid);
		schema1.setsState(1);//state 1：为已办理   0:未办理
		map.put("state", schemaService.updateByExample(schema1));
		return map;
	}
	
	
	/** 
	 * 注册时间类型的属性编辑器，将String转化为Date 
	 */  
//	@InitBinder  
//	public void initBinder(ServletRequestDataBinder binder) {  
//	    binder.registerCustomEditor(Date.class, new CustomDateEditor(  
//	            new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"), true));  
//	}
}
